<?php
namespace app\index\controller;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods:POST,GET");

use app\common\controller\HomeBase;
use app\index\model\Secondhandle as Second;
use think\Db;
use think\Config;

class Register extends HomeBase
{
	protected function _initialize(){
		parent::_initialize();
	}

	/**
	*手机号验证码
	*@param   $phone		 注册手机的手机号码
	**/
	public function getMessage()
	{
		$phone = $_REQUEST['phone'];
		// $phone = 15225140805;
		$code = rand(100000,999999);
		//获取号码数组
		$photos = Db('user')->field('phone')->select();
		$i = 0;
		for(;$i < count($photos);$i++){
			$arr[$i] = $photos[$i]['phone'];
		}
		//判断号码是否已经注册过了
		if(in_array($phone,$arr)){
			$this->apiError('该手机号已经注册过');
		}else{
			//发送验证码
			$list = $this->set_phone($phone,$code);
			$sid = $list['sid'];
			$data = [
				'code'=>$code,
				'phone'=>$phone,
				'sid'=>$sid
			];
			$add = Db('sms')->insert($data);
			$sidlist['sid'] = $sid;
			if($add){
				$this->apiSuccess($sidlist);
			}else{
				$this->apiError('发送失败,请重试');
			}
		}
	}

	/**
	*手机号用户注册
	*@param   $phone		 注册手机的手机号码
	**/
	public function register()
	{
		//获取表单穿过来的数据
		$phone = $_POST['phone'];
		$sid = $_POST['sid'];
		$code = $_POST['code'];
		$password = $_POST['password'];
		$password = md5($password . Config::get('salt'));
		//判断输入的短信验证码是否正确
		$list = Db('sms')->where('phone',$phone)->where('sid',$sid)->select();
		//判断是否发送短信验证
		if(count($list)){
			//判断验证码是否正确
			if($list[0]['code'] == $code){
				//入库
				$name = $this->generate_password();
				$create_time = date('Y-m-d H:i:s',strtotime('now'));
				$data = [
					'name'=>$name,
					'phone'=>$phone,
					'password'=>$password,
					'applicationDate'=>$create_time,
				];
				$add = Db('user')->insert($data);
				//判断是否添加成功
				if($add){
					$this->apiSuccess($add);
				}else{
					$this->apiError('添加失败,请重试');
				}
			}else{
				$this->apiError('验证码错误');
			}
		}else{
			$this->apiError('发送短信错误');
		}
	}

	// 以下为核心代码部分
	protected function set_phone($phone,$coding){
		// 必要参数
		$apikey = "cfe9053773f4bf081bca73c9b9fe9ece";
		$mobile = trim($phone);
		$text="【东升物流】您的验证码是".$coding."。如非本人操作，请忽略本短信";
		$url = 'https://sms.yunpian.com/v2/sms/single_send.json';
		
		// 发送短信
		$data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);
		$json_data = $this->https_request($url,$data);

		$array = json_decode($json_data,true);
		return $array;
	}

	// cURL函数简单封装
	protected function https_request($url, $data = null)
	{
		 $curl = curl_init();
		 curl_setopt($curl, CURLOPT_URL, $url);
		 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
		 if (!empty($data)){
		  	curl_setopt($curl, CURLOPT_POST, 1);
		  	curl_setopt($curl, CURLOPT_POSTFIELDS,http_build_query($data));
		 }
		 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		 $output = curl_exec($curl);
		 curl_close($curl);
		 return $output;
	}


	protected function generate_password( $length = 6 ) { 
		$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 
		$password = ''; 
		for ( $i = 0; $i < $length; $i++ ) 
		{		
			$password .= $chars[ mt_rand(0, strlen($chars) - 1) ]; 
		} 
		return $password; 
	} 

}